package com.lry.util.requestcore;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.executor.requestcore.domain.request.GRequest;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

/**
 * @author ALI
 * @since 2023/7/14
 */
@Component
public class DefaultGRequestXxlJobHandler {

    @Resource(name = "scsyjtJdbcTemplate")
    private JdbcTemplate sjdbcTemplate;

    @Resource(name = "bizJdbcTemplate")
    private JdbcTemplate bjdbcTemplate;

    /**
     * 公共的Jobhandler
     */
    @XxlJob("commonGJobHandler")
    public void handler() {
        String param = XxlJobHelper.getJobParam();
        GRequest request = JSON.parseObject(param, GRequest.class);
        JSONObject jsonObject = JSON.parseObject(param);
        String jdbcType = jsonObject.getString("jdbcType");
        GRequestDealClient client = new DefaultGRequestDealClient(getJdbcTemplate(jdbcType));
        client.request(request);
    }

    private JdbcTemplate getJdbcTemplate(String jdbcType) {
        if (StringUtils.isBlank(jdbcType)) {
            return sjdbcTemplate;
        }
        if (jdbcType.startsWith("scsyjt")) {
            return sjdbcTemplate;
        }
        if (jdbcType.startsWith("biz")) {
            return bjdbcTemplate;
        }
        return sjdbcTemplate;
    }
}
